#include <avr/wdt.h>
#include <Timer.h>
#include "Wire.h"
#include "ClickButton.h"

const int buttons = 10;

int buttonPin[buttons] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int switchfunction[buttons]  = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int switchState[buttons]     = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte outputnode[buttons];
byte outputpin[buttons];
byte type[buttons];

ClickButton button[10] = {
  ClickButton (buttonPin[1], LOW, CLICKBTN_PULLUP),
  ClickButton (buttonPin[2], LOW, CLICKBTN_PULLUP),
  ClickButton (buttonPin[3], LOW, CLICKBTN_PULLUP),
  ClickButton (buttonPin[4], LOW, CLICKBTN_PULLUP),
  ClickButton (buttonPin[5], LOW, CLICKBTN_PULLUP),
  ClickButton (buttonPin[6], LOW, CLICKBTN_PULLUP),
  ClickButton (buttonPin[7], LOW, CLICKBTN_PULLUP),
  ClickButton (buttonPin[8], LOW, CLICKBTN_PULLUP),
  ClickButton (buttonPin[9], LOW, CLICKBTN_PULLUP),
  ClickButton (buttonPin[0], LOW, CLICKBTN_PULLUP),
};


#define DEBUG
#define LED         4
#define DELAY1S     1000
#define DELAY5S     5000
#define MAX         4090
#define MIN         0

#define THIS_ADDRESS 0x8

void setup() {
  Serial.begin(9600);
  Serial.println("Starting...");
  Wire.begin(THIS_ADDRESS);
  Wire.onReceive(receiveEvent);
  Serial.println("config buttons....");
  for (int i=0; i<buttons; i++)
  {
     // Setup button timers (all in milliseconds / ms)
    // (These are default if not set, but changeable for convenience)
    button[i].debounceTime   = 20;   // Debounce timer in ms
    button[i].multiclickTime = 250;  // Time limit for multi clicks
    button[i].longClickTime  = 1000; // Time until long clicks register
  }
  Serial.println("Config done");
  
}

void loop() {
for (int i=0; i<buttons; i++)
  {
    // Update state of all buitton
    button[i].Update();
  
    // Save click codes, as clicks counts are reset at next Update()
    if (button[i].clicks != 0) switchfunction[i] = button[i].clicks;
    
    if(button[i].clicks == 1) 
      {
        switchState[i] = !switchState[i];
        if (switchState == LOW) {
          SendState(outputnode[i], outputpin[i], MIN);
      } else {
          SendState(outputnode[i], outputpin[i], MAX);
      }
     }
    if(switchfunction[i] == 2) ;
    if(switchfunction[i] == 3) ;
    if(switchfunction[i] == -1) 
    {
      switchState[i] = !switchState[i];
        if (switchState == LOW) {
          SendState(outputnode[i], outputpin[i], MIN);
      } else {
          SendState(outputnode[i], outputpin[i], MAX);
      }
    }
    if(switchfunction[i] == -2) ;
    if(switchfunction[i] == -3) ;
  }
}

void receiveEvent(int howMany)
{
  Serial.println("Config ontvangen...");
  while (1 < Wire.available()) // loop through all but the last
  {
    int inputpin = Wire.read(); // receive int
    outputnode[inputpin] = Wire.read();    // receive int
    outputpin[inputpin] = Wire.read();     
    type[inputpin] = Wire.read();
  }
  Serial.println("config ontvangen");
}

void SendState(byte outputnode,byte outputpin,byte state)
{
   Serial.println("status verzenden...");
    Wire.beginTransmission(outputnode); // transmit to device #4
    Wire.write(outputpin);        // sends five bytes
    Wire.write(state);
    Wire.endTransmission();    // stop transmitting
   Serial.println("Alles verzonden");
   Serial.print("outputnode = ");
   Serial.println(outputnode);
   Serial.print("outputpin = ");
   Serial.println(outputpin);
   Serial.print("status ");
   Serial.println(state);
 }
